<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
				</div>
				<div class="flex-end-center-wrap">
					<jk-date-picker v-model="searchParams.belongDate" :picker-options="pickerOptions" placeholder="日期" size="small" class="search-bar-item-size2 margin-5-0 item-gap" clearable />
					<jk-search-input v-model="searchParams.materialNameOrCode" placeholder="原料名称或编码" size="small" class="search-bar-item-size2 margin-5-0 item-gap" clearable />
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>

		<jk-card>
			<jk-table
				:loading="tableLoading"
				:data="tableData"
				show-footer
				:footer-method="footerMethod"
				:height="tableHeight + 10"
			>
				<vxe-column
					v-for="(item, index) in tableColumnList"
					:key="index"
					:title="item.title"
					:align="item.align"
					:field="item.keyName"
					:sortable="item.sortable"
					:fixed="item.fixed ? item.fixed : ''"
					:min-width="item.minWidth"
					:width="item.width"
				>
					<template slot-scope="scope">
						<jk-state-bar v-if="item.keyName === 'auditState'" :state-list="packageStateList" :state="scope.row.auditState"></jk-state-bar>
						<span v-else-if="item.keyName === 'maxValue'">{{ `${scope.row.minValue}~${scope.row.maxValue}` }}</span>
						<div v-else-if="item.keyName === 'processList'">
							<div
								v-for="(processItem, processIndex) in scope.row.configItem || []"
								:key="processIndex"
							>{{ `${processIndex + 1}、${processItem.processName}: ${processItem.unitPrice}` }}</div>
						</div>
						<span v-else>{{ scope.row[item.keyName] }}</span>
					</template>
				</vxe-column>
			</jk-table>

			<!-- 分页组件 -->
			<footer ref="box2" class="flex-end-center margin-top-10">
				<jk-page
					:page-index="searchParams.pageNo"
					:page-sizes="searchParams.pageSizes"
					:page-size="searchParams.pageSize"
					:total="totalCount"
					@on-change="onPageChange"
				/>
			</footer>
		</jk-card>
	</div>
</template>

<script>
    import calHeight from '/src/mixins/cal-table-height';
    import { pageConfig, formatDate, tipsBar } from '/src/utils/jk/jk-tool';
    import { systemUserColumnConfigGet } from '@/api/system/column-config';
    import getMenuColumn from '../../../mixins/getMenuColumn';
    import XEUtils from 'xe-utils';
    import { productionMaterialInventoryDayData } from '../../../api/system/leaveEnterWarehouseReportFormsHH';

    export default {
        name: 'LeaveEnterWarehouseReportFormsHH',
        components: {},
        mixins: [calHeight, getMenuColumn],
        data() {
            return {
                apiItem: {
                    'page': productionMaterialInventoryDayData
                },
                pageType: '',
                prefix: 'production:packageInStorage',
                dataId: null,
                packageStateList: [
                    { id: 0, name: '待提交', color: '#ff9900' },
                    { id: 1, name: '已提交', color: '#2b85e4' },
                    { id: 2, name: '已入库', color: '#19be6b' }
                ],
                saveDialogState: false,
                tableLoading: true,
                exportLoading: false,
                tableData: [],
                totalCount: 0,
                noCalKey: ['materialCode', 'materialName'],
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() > Date.now();
                    }
                },
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    materialNameOrCode: '',
                    belongDate: ''
                }
            };
        },
        mounted() {
            this.getDependentData();
        },
        methods: {
            onStateChange(e) {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            getMenuColumnListRequest() {
                return systemUserColumnConfigGet({ path: 'LeaveEnterWarehouseReportFormsHH', filterShow: true }).then(res => {
                    if (res.data.status === 200) {
                        this.tableColumnList = res.data.res.columnConfigList && res.data.res.columnConfigList.length ? res.data.res.columnConfigList[0].itemList : [];
                    }
                });
            },
            footerMethod({ columns, data }) {
                return [
                    columns.map((column, columnIndex) => {
                        if (columnIndex === 0) {
                            // return `合计: ${this.totalData.totalOutputM || 0}M; ${this.totalData.totalOutputKg || 0}KG`;
                            return `合计`;
                        }
                        if (!this.noCalKey.includes(column.property)) {
                            return XEUtils.sum(data, column.property);
                        }
                        return null;
                    })
                ];
            },
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            async getDependentData() {
                this.tableLoading = true;
                if (!this.searchParams.belongDate) {
                    this.$set(this.searchParams, 'belongDate', formatDate(Date.now()));
                }
                await this.getMenuColumnListRequest();
                await this.getListRequest();
                this.tableLoading = false;
            },
            onDetailBtn(e) {
                this.pageType = 'detail';
                this.dataId = e.id;
                this.saveDialogState = true;
            },
            onFinish() {
                this.saveDialogState = false;
                this.getListRequest();
            },
            onVisibleChange(e) {
                this.saveDialogState = e;
                if (!e) {
                    this.dataId = null;
                }
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                return this.apiItem['page'](this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res;
                        this.totalCount = res.data.count;
                    }
                });
            },
            /** 新增按钮操作 */
            onAddBtn() {
                this.pageType = 'add';
                this.saveDialogState = true;
            },
            /** 修改按钮操作 */
            onEditBtn(row) {
                this.pageType = 'edit';
                this.dataId = row.id;
                this.saveDialogState = true;
            }
        }
    };
</script>
